/*
Inputs:  > prepared precinct panel [newdata/estimation_prep_ltw18]
		 > arc_anly/arc_output/ltw18_sb_intersected 
			[Outcome of local matching (neighboring precincts), performed in ArcGIS]
			
Output: Figure C.3, C.4, C.5
			
Tasks:
		Event study analysis on matched samples using
		- 	Local matching
		-	Mahalanobis distance matching
		- 	Propensity score matching
		- 	Entropy Balancing
		
*/	
	
* PULL: Precinct-level data
	use "$newdata/estimation_prep_ltw18.dta", clear
	
	
********************************************************************************
	//	 Prep Estimation //
********************************************************************************


*** Prep sample for local matching ---------------------------------------------		
frame copy default tmp, replace 
frame tmp{		
* PULL: Precinct-level matched sample (with replacement) from ArcGIS
*	k nearest neighbors where k=0 if treated unit is surrounded only by other treated units
*		and k>=1 if treated unit shares precinct border with at least one never-treated (control) unit
*	sb_new := ID of treated precincts with matches (some treated units have no matched controls)
*	sb_new_1 := IDs control precincts matched to treated precincts 

	import dbase "$arc_anly/arc_output/ltw18_sb_intersected", clear
	
	// drop duplicates (some treated and control units share more than one edge)
	duplicates drop
	
	// gen lmatch_id := ID for cluster of treated unit and matched control units 
	gen 	 lmatch_id = sb_new
	lab var  lmatch_id "ID for 1:k matches of local matching"
	
	// save list of distinct treated units with matches (N=248)
	tempvar tmp 
	tempfile distinct_T
	bys sb_new: gen `tmp'= (_n==1)
	savesome sb_new lmatch_id T if `tmp'==1 using `distinct_T'
		
	// List of matched control precincts
	keep sb_new_1 lmatch_id T_1
	ren  sb_new_1 sb_new 
	ren	 T_1 T   
	
	// append distinct treated units 
	append using `distinct_T'
	lab var T "indicator for treated units"
	
	// gen district id
	gen stadtbez = floor(sb_new/100)
	gen lm_stadtbez = floor(lmatch_id/100)
	
	// merge with data
	expand  8 // expand to match 8 elections
	bys sb_new lmatch_id: gen wahl_id=_n

	// gen sample ID of local matching 
	gen smpl_lmatch = 1
	lab var smpl_lmatch "ID treated/control units of local matching"
	
	// save 1:k (adjacent) neighbor matching
	save "$tmp/local_matching_sample_k.dta", replace
	
	// save 1:1 neighbor matching (simply ids treated and control units)
	//		Note: may include control units matched to treated units in DIFFERENT district
	//		Note: for untreated units matched to several treated, lmatch_id is randomly assigned
	tempvar tmp1 
	bys sb_new wahl_id: gen `tmp1'= (_n==1)	
	drop lmatch_id
	savesome sb_new T smpl_lmatch wahl_id if `tmp1'==1 using "$tmp/local_matching_sample_1.dta", replace
	cap drop `tmp1'
	
	// save 1:1 neighbor matching excluding cases where control unit is matched to treated in DIFF district 
	drop if lm_stadtbez !=stadtbez
	
	duplicates drop sb_new wahl_id, force 
	drop *stadtbez
	
	save "$tmp/local_matching_sample_1_within_district.dta", replace
	
} 
	
*-------------------------------------------------------------------------------

*** Prep sample
	// compute id for DISTANCE increase/decrease, 0 else
	cap drop tmp*
	gen 	tmp = (del_street_dist>0) 			if K==0
	bys sb_new (tmp): gen ind_dist_up = tmp[1]
	replace ind_dist_up = 0 					if missing(Ei)
	lab var ind_dist_up "=1 if dist increase in event, 0 else"
	
	cap drop tmp*
	gen 	tmp = (del_street_dist<0) 			if K==0
	bys sb_new (tmp): gen ind_dist_dn = tmp[1]
	replace ind_dist_dn = 0 					if missing(Ei)
	lab var ind_dist_dn "=1 if dist decrease in event, 0 else"		
	

* TWFE OLS
	// gen leads and lags
	cap drop L* F*
	forvalues l = 7(-1)1 {
		gen F`l'event = K==-`l'
	}	
	forvalues l = 0/7 {
		gen L`l'event = K==`l'
	}
	// Create two set of dummies: Reason Dummy x event-time dummies
	forvalues l = 7(-1)1 {		
		gen 	F`l'event_a = F`l'event *ind_dist_dn				// a := decrease
		lab var F`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t-`l'#)"
		gen	 	F`l'event_b = F`l'event *ind_dist_up				// b:= increase
		lab var F`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t-`l'#)"

		assert  F`l'event_b+F`l'event_a==F`l'event		
	}
	forvalues l = 0/7 {		
		gen 	L`l'event_a = L`l'event *ind_dist_dn	// a := decrease
		lab var L`l'event_a "(N-)x\hspace{.7cm}Reassignment (#t+`l'#)"
		gen	 	L`l'event_b = L`l'event *ind_dist_up	// b:= increase
		lab var L`l'event_b "(N+)x\hspace{.7cm}Reassignment (#t+`l'#)"
		assert  L`l'event_b+L`l'event_a==L`l'event
	}		
	
	// ORDER dummies
	order *event_b, last
	order F1event*,last		
	
	
 *** Prepare weights and matched samples based on PSM, Mahanabolis, and Entropy bal
 * Note: since we match on pre-treatment outcomes, we drop the 10 precincts that were
 * 			treated in the first eleciton in our panel
	
	
// Balancing vars  	
global ebal ew_ges ew_biodt ew_dtmihi ew_ledig ew_married wb_anteil ///
			wb_18t24 wb_25t34 wb_35t44 wb_45t59 avg_dur hh_kids mpreis_flats_rent ///
				share_mail turnout_tot_req  shr_spd ///
				street_dist	
	
 * 1) Entropy weights : fits weights s.t. 1st & 2nd moments of cov. distr of NT match T
 *		> Exclude units treated in 2013 (no pre-treatment observations)
 *		> include #eligible voters as 'base weights' for regressions
	
	local ncov :word count $ebal
	local nt = "2 "*`ncov'
	
	ebalance T $ebal if wahl_id==1 &Ei!=1, wttreat basewt(wahlber_gesamt) target(`nt')

	grconst _webal, by(sb_new) fill
	
	
	
 * 2) PSM: 1:1 matching (K:1 nearest neighbor) with propensity score matching (no replacement)
	// generates several variables:
	//	_weight:  frequency with which the obs is used as a match (here: 1 or . ), ids treated and matched control units
	// _id : "new" identifyer for all obs	
	// _n1: ID of of nearest neighbor nr. 1
	// _pdif: | pscore - ps of nearest neighbor | (essentially distance measure)
	// _nn: #matched neighbors
	
	// compute propensity score, excluding units treated in 2013 (gen: pscore)
	cap drop pscore
	probit T $ebal if wahl_id==1 & Ei!=1, r 
	predict pscore if wahl_id==1 & Ei!=1
	
	* define caliper
	sum pscore
	local cal `r(sd)'/5
	di `cal'
	* common support
	sum pscore if T==0
	gen cmmn_smpl = pscore<=`r(max)'
	return clear
	sum pscore if T==1
	di `r(min)'
	replace cmmn_smpl = 0 if pscore<`r(min)'-0.00001
	// pscore comes from regression above
	psmatch2 T if wahl_id==1 &Ei!=1 & cmmn_smpl==1, pscore(pscore) noreplacement

	* smpl_psm: PSM matched sample
	bys sb_new: egen smpl_psm = mean(_weight)
	replace smpl_psm=0 if smpl_psm==.
	lab var smpl_psm "PSM matched sample id"
	assert inlist(smpl_psm,0,1)

	
	*drop generated variables from "psmatch2" to do mahalanobis matching next
	drop _pscore _treated _support _weight _id _n1 _nn _pdif

* 3) Local matching (different frame since some control units are duplicated )
	
	// 1:k matching without replacement local_matching_sample_1_within_district
	merge 1:1 wahl_id sb_new using "$tmp/local_matching_sample_1_within_district.dta", assert(1 3) nogen 
	
	// new frame for 1:k matching with replacement (several controls to one treated unit)
	frame copy default frlm, replace 
	frame frlm: merge 1:m sb_new wahl_id using "$tmp/local_matching_sample_k.dta", assert(1 3) nogen 
	frame frlm: bys sb_new wahl_id: gen lm_unique = (_n==1)	// ids unique controls 

* 4) Mahalanobis 1 NN matching with replacement
*	// note: untreated units matched to multiple treated units are duplicated
*		+ TW clustering following Colmer et al (Restud, 2024) 
*	// note: matching w/o replacement is only implemented with 1:1 PSM
	psmatch2 T if wahl_id==1 &Ei!=1,  mahalanobis($ebal) 
	
	* smpl_maha : mahanabolis matched sample
	cap drop tmp*
	bys sb_new: egen tmp = mean(_weight)
	gen smpl_maha = !missing(tmp)
	assert inlist(smpl_maha,0,1)
	
	*generate ID of matched untreated precincts
	gen 	mm_ctr_id = _n1
	replace mm_ctr_id = _id if mm_ctr_id==. & _weight!=.
	grconst mm_ctr_id, by(sb_new) fill	
	lab var mm_ctr_id "MM: untreated unit ID"		


* Gen pair match identifyer, duplicate multiple matched untreated units
frame copy default tmp, replace 
frame tmp{
	keep if wahl_id==1
	
	*generate duplicates for all control precincts matched more than once
	expand _weight
	
	*tmp_merger is a variable used for the merge
	bys sb_new: gen tmp_merge=_n
	
	*gen matched pari ID twc3 is matched pairs ID
	bys 	mm_ctr_id _treated: gen tmp1 = mm_ctr_id + _n
	egen 	mm_pair_id = group(mm_ctr_id tmp1)
	lab var mm_pair_id "MM: matched pair ID"
	
	keep sb_new tmp_merge mm_pair_id
	
	tempfile maha_matches 
	save `maha_matches'
}	


 * gen new frame for mahanabolis matched sample (duplicate units)	
frame copy default frmaha, replace
frame frmaha{
		
	*gen duplicates of all control precincts matched more than once (also for the whole sample)
	grconst _weight, by(sb_new) fill	
	expand _weight
	bys sb_new wahl_id: gen tmp_merge=_n

	merge m:1 sb_new tmp_merge using `maha_matches', keepusing(mm_pair_id) assert(3)
	
	bys mm_pair_id: assert _N==16 if smpl_maha==1

}	

********************************************************************************
		// Matching: Baseline Specification (Figure C3 //
********************************************************************************	

	
		estimates clear
	
	// Mahalanobis distance matching
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {		
		// untreat units matched to several treated duplicates + TW cluster by pairmatch and untreated unit	
		frame frmaha: reghdfe `v' F7event-L7event F1event $ctr [aw=wahlber_gesamt] if smpl_trim==1 & smpl_maha==1 & Ei!=1, ///
			 absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(mm_pair_id mm_ctr_id#i.wahl_id)
		
		estimates store `v'_maha
	}

	// Propensity score matching
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		 reghdfe `v' F7event-L7event F1event $ctr [aw=wahlber_gesamt] if smpl_trim==1 & smpl_psm==1 & Ei!=1, ///
			absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'_psm
	}
	
	// Entropy balancing
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		 reghdfe `v' F7event-L7event F1event $ctr [aw=_webal] if smpl_trim==1 & Ei!=1, absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		
		estimates store `v'_ebal
	}		
	
	// Local matching 
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
		 reghdfe `v' F7event-L7event F1event $ctr [aw=wahlber_gesamt] if smpl_trim==1 & smpl_lmatch==1 &Ei!=1, ///
			absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)
		estimates store `v'_lm
	}
	

** PLOT 
	// set legend 
	local lorder `" order(1 "Local matching" 3 "Propensity score matching" 5 "Mahalanobis distance matching"  7 "Entropy balancing weights" )"'
			
	// PLOT: Turnouts Urne 
	local d = "turnout_urne"
	event_plot `d'_lm `d'_psm `d'_maha `d'_ebal, ///
	stub_lag(L#event ) stub_lead(F#event ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `lorder' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel A.} Effect on Polling Place Turnout",nobox span bexpand justification(left) size(medium)) ///
		name(urne, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry))  ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))
	
	// PLOT: Turnouts Postal
	local d = "turnout_pos_req"
	event_plot    `d'_lm `d'_psm `d'_maha `d'_ebal, ///
	stub_lag(L#event ) stub_lead(F#event ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `lorder' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel B.} Effect on Mail-in Turnout",nobox span bexpand justification(left) size(medium)) ///
		name(postal, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry)) 	///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))

	// PLOT: Turnouts Total
	local d = "turnout_tot_req"
	event_plot  `d'_lm `d'_psm `d'_maha `d'_ebal, ///
	stub_lag(L#event ) stub_lead(F#event ) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `lorder' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		title("{bf:Panel C.} Effect on Total Turnout",nobox span bexpand justification(left) size(medium)) ///
		name(total, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry)) ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))	
		
	* PLOT: FIGURE C3. Robustness to Matching on Observables–Pooled Reassignments
	grc1leg2  urne postal total , xcommon  col(2) iscale(.7) name(bsl_rob_matching, replace) ///
	pos(4) ring(0) lcol(1) ltitle("Matching approach:")  ///
				lxoffset(-8) lyoffset(15) legscale(*.9) ltsize(*.8)
	gr_edit .legend.title.DragBy 0 -8
	graph export "$figures/Figure_C3_ES_matching_bsl.pdf", replace
	
	
		
	
********************************************************************************
	// Matching: Heterogeneity by Increase/Decrease in distance (Figure C4) //
********************************************************************************		
	
	estimates clear	
	
	// maha distance matching	
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
	 frame frmaha: reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b  $ctr [aw=wahlber_gesamt] if smpl_trim==1 & smpl_maha==1 & Ei!=1, ///
		absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(mm_pair_id mm_ctr_id#i.wahl_id)


		estimates store `v'_a_maha
		estimates store `v'_b_maha
	}		
	
	// propensity score matching	
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b  $ctr [aw=wahlber_gesamt] if smpl_trim==1 & smpl_psm==1 & Ei!=1, ///
			absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)


		estimates store `v'_a_psm
		estimates store `v'_b_psm
	}		
		
	// Local Matching 
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		qui reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b $ctr [aw=wahlber_gesamt] ///
					if  smpl_trim==1 & smpl_lmatch==1 , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)


		estimates store `v'_a_lm
		estimates store `v'_b_lm	
	}		
	
	// Entropy balancing 
	foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req {
	
		qui reghdfe `v' F7event_a-L7event_a  F7event_b-L7event_b F1event_a F1event_b $ctr [aw=_webal]  ///
					if  smpl_trim==1 & Ei!=1 , absorb(i.wahl_id#i.stadtbez i.sb_new) cluster(sb_new)


		estimates store `v'_a_ebal
		estimates store `v'_b_ebal	
	}		
	
** PLOT Results	
	
 foreach v of varlist turnout_urne turnout_pos_req turnout_tot_req { 
		
	// set legend 
	local lorder `" order(1 "Local matching" 3 "Propensity score matching" 5 "Mahalanobis distance matching"  7 "Entropy balancing weights" )"'
	
		// PLOT Dist Decrease
		event_plot  `v'_a_lm `v'_a_psm `v'_a_maha  `v'_a_ebal , ///
		stub_lag(L#event_a ) stub_lead(F#event_a) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `lorder' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		subtitle("{bf:b.} Distance decrease",nobox justification(left) size(medsmall)) ///
		name(`v'_dwn, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry)) ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))
	
		// PLOT Dist Increase
		event_plot   `v'_b_lm `v'_b_psm `v'_b_maha  `v'_b_ebal , ///
		stub_lag(L#event_b ) stub_lead(F#event_b) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.24(0.16)0.24) trimlead(4) trimlag(2) noautolegend ///
	graph_opt(ytitle("Voter turnout in %""(estimates)", size(medsmall)) xlabel(-4(1)2) xtitle("Election since reassignment", size(medsmall)) ///
		legend(pos(12) `lorder' row(1) region(style(none))) ///
		xline(-0.5, lcolor(black) lpattern(solid)) yline(0, lcolor(gray) lpat(solid)) ylabel(, angle(horizontal)) ///
		subtitle("{bf:a.} Distance increase",nobox justification(left) size(medsmall)) ///
		name(`v'_up, replace) ) ///
	lag_opt1(msymbol(S) msize(3pt) color(teal))  lag_ci_opt1(color(teal)) ///
	lag_opt2(msymbol(+) msize(3pt) color(black))  	lag_ci_opt2(color(black)) ///
	lag_opt3(msymbol(Sh) msize(3pt) color(cranberry)) 	lag_ci_opt3(color(cranberry)) ///
	lag_opt4(msymbol(Oh) msize(3pt) color(midblue)) 	lag_ci_opt4(color(midblue))  ///
	lag_opt5(msymbol(Th) msize(3pt) color(forest_green)) lag_ci_opt5(color(forest_green))
	
	
 }	

		
	* PLOT: FIGURE C4. Robustness to Matching on Observables–Effects by Distance Change
	grc1leg2 turnout_urne_up turnout_urne_dwn, 		 name(g1, replace) ///
			title("{bf:Panel A.} Effect on Polling Place Turnout", just(left) bexpand size(small)) loff  iscale(.7) imargins(small)
	grc1leg2 turnout_pos_req_up turnout_pos_req_dwn, name(g2, replace) ///
			title("{bf:Panel B.} Effect on Mail-in Turnout", just(left) bexpand size(small)) loff  iscale(.7) imargins(small)
	grc1leg2 turnout_tot_req_up turnout_tot_req_dwn, name(g3, replace) ///
			title("{bf:Panel C.} Effect on Total Turnout", just(left) bexpand size(small))   iscale(.7) imargins(small)
	grc1leg2 g1 g2 g3, col(1) imargins(zero) legscale(*.65) lrow(2)
	gr_edit .style.editstyle declared_ysize(6) editcopy	
	
	graph export "$figures/Figure_C4_ES_matching_dist2.pdf", replace 
	
	
********************************************************************************
// Visualization of local matching outcome: MAP of matched precincts (Figure C5) //
********************************************************************************	

** Plot: Local Matching, treated vs. matched control units	
use "$newdata/estimation_prep_ltw18.dta", clear	

	// merge with local matched sample 
	merge 1:1 wahl_id sb_new using "$tmp/local_matching_sample_1_within_district.dta", assert(1 3) nogen 

	// keep any election 
	keep if wahl_id==1
	
	merge 1:n sb_new using "$newdata/ltw18_sb_mapping.dta", assert(3) nogen
	
	// gen help variable 1=matched treated, 2 = matched control 3 = not matched 
	gen 	help = 1 if T==1 &smpl_lmatch==1 
	replace help = 2 if T==0 &smpl_lmatch==1 
	replace help = 3 if smpl_lmatch==.
	
	
	* PLOT: FIGURE C5. Map of Matched Treated and Control Precincts
	spmap help using "$shp_path/landtagwahl2018_stimmbezirke_newprj_shp.dta", ///
	id(_ID)  /*clmethod(quantile) cln(10)*/ clmethod(unique) fcol(navy%40 cranberry%60 white) ///
	osize(vthin ...) ocol(black%20 ...) /* ndsize(vthin) ndocol(black%40) ndfcolor(none)*/ ///
	polygon(data("$shp_path/stadtbezirke_shp.dta") ocol(black ..) osize(*1 ..) fcol(none ..) opat(solid ..)) ysize(3)  ///
	legend( pos(8) order(2 "Matched {bf:treated} precincts" 3 "Matched {bf:control} (never-treated) precincts"  4 "Unmatched units" )  )
	gr_edit .legend.DragBy 10 -9
	graph export "$figures/Figure_C5_map_local_matching_smpl.pdf", replace	
	
		